﻿// GlueSNCheckDlg.cpp: 实现文件
//

#include "pch.h"
#include "ProjectAA.h"
#include "afxdialogex.h"
#include "GlueSNCheckDlg.h"


// CGlueSNCheckDlg 对话框

IMPLEMENT_DYNAMIC(CGlueSNCheckDlg, CDialogEx)

CGlueSNCheckDlg::CGlueSNCheckDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_GLUESNCHECK, pParent)
	, SNInput(_T(""))
{

}

CGlueSNCheckDlg::~CGlueSNCheckDlg()
{
}

void CGlueSNCheckDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT1, SNInput);
}


BEGIN_MESSAGE_MAP(CGlueSNCheckDlg, CDialogEx)
	ON_BN_CLICKED(IDOK, &CGlueSNCheckDlg::OnBnClickedOk)
	ON_BN_CLICKED(IDCANCEL, &CGlueSNCheckDlg::OnBnClickedCancel)
	ON_WM_TIMER()
END_MESSAGE_MAP()


// CGlueSNCheckDlg 消息处理程序

void CGlueSNCheckDlg::OnBnClickedOk()
{
	CString cstr;
	char CodeSet[200];
	char CodeInput[200];
	USES_CONVERSION;

	if (SNCheckStep == 0) {
		//判断胶水SN
		UpdateData(TRUE);

		cstr.Format(L"获取到的胶水SN:[%s]，设定的SN:[%s]", SNInput, gSystemMgr.GlueSNCheck);
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);

		sprintf(CodeInput, "%s", T2A(SNInput));
		sprintf(CodeSet, "%s", T2A(gSystemMgr.GlueSNCheck));
		cstr.Format(L"[%d]，[%d]", strlen(CodeSet), strlen(CodeInput));
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);

		for (int i = 0; i < strlen(CodeSet); i++) {
			if (CodeSet[i] != CodeInput[i]) {
				SNInput = L"";
				UpdateData(FALSE);

				gSystemLog.DisplayLogAndSave(L"胶水条码比对NG", LV_EVENT);
				MessageBox(L"胶水条码比对NG", L"胶水确认", MB_ICONERROR);
				return;
			}
		}

		SNInput = L"";
		UpdateData(FALSE);
		gSystemLog.DisplayLogAndSave(L"胶水条码比对OK", LV_EVENT);
		SNCheckStep = 1;
		return;
	}

	if (SNCheckStep == 1) {
		//判断螺杆阀SN
		UpdateData(TRUE);

		cstr.Format(L"获取到的螺杆阀SN:[%s]，设定的SN:[%s]", SNInput, gSystemMgr.LgfSNCheck);
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);

		sprintf(CodeInput, "%s", T2A(SNInput));
		sprintf(CodeSet, "%s", T2A(gSystemMgr.LgfSNCheck));

		for (int i = 0; i < strlen(CodeSet); i++) {
			if (CodeSet[i] != CodeInput[i]) {
				SNInput = L"";
				UpdateData(FALSE);

				gSystemLog.DisplayLogAndSave(L"螺杆阀条码比对NG", LV_EVENT);
				MessageBox(L"螺杆阀条码比对NG", L"螺杆阀确认", MB_ICONERROR);
				return;
			}
		}

		gSystemLog.DisplayLogAndSave(L"螺杆阀条码比对OK", LV_EVENT);
		gSystemMgr.m_bAutoFlag_20 = 1;
		gSystemMgr.m_cIo.SetOutputIo(29, ON);
		cstr.Append(L"点胶开启自动");
		gSystemLog.DisplayLogAndSave(cstr.GetBuffer(), LV_EVENT);
		//GetDlgItem(IDC_CHECK_10AUTO)->SetWindowTextW(L"点胶");

		CDialogEx::OnCancel();
	}

}

void CGlueSNCheckDlg::OnBnClickedCancel()
{
	// TODO: 在此添加控件通知处理程序代码
	CDialogEx::OnCancel();
}

void CGlueSNCheckDlg::OnCancel()
{
	KillTimer(TIMER_ID_GLUESNCHECK);

	CDialogEx::OnCancel();
}

void CGlueSNCheckDlg::OnTimer(UINT_PTR nIDEvent)
{
	if (nIDEvent == TIMER_ID_GLUESNCHECK)
	{
		if (SNCheckStep == 0) {
			GetDlgItem(IDC_STATIC_1)->SetWindowTextW(L"请扫描胶水二维码:");
		}

		if (SNCheckStep == 1) {
			GetDlgItem(IDC_STATIC_1)->SetWindowTextW(L"请扫描螺杆阀二维码:");
		}

	}

	CDialogEx::OnTimer(nIDEvent);
}

BOOL CGlueSNCheckDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	SetTimer(TIMER_ID_GLUESNCHECK, 100, NULL);

	return TRUE;
}
